package com.tucc.hello;

import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.Map;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello
 * @date 2021/6/17
 **/
public class Test {

    public static void main(String[] args) {

        System.out.println(monthsBetween(20210228, 20210130));
        System.out.println(monthsBetween(20210228, 20210128));
        System.out.println(monthsBetween(20210228, 20210131));
        System.out.println(monthsBetween(20210331, 20210228));


        System.out.println(betweenMonthes(20210130, 20210228));
    }

    public static BigDecimal monthsBetween(Integer minuend, Integer subtraction) {
        if (minuend <= 9999 || subtraction <= 9999) {
//            Ta5Logger.error(logger,
//                    "非法年月日:minuend:" + minuend + ",subtraction:" + subtraction + ",将返回 0");
            return BigDecimal.ZERO;
        }
        BigDecimal integerPart = BigDecimal
                .valueOf(betweenMonthes(minuend, subtraction));
        if (smaller(minuend, subtraction)) {
            integerPart = integerPart.negate();
        }
        BigDecimal minuendDays = BigDecimal.valueOf(minuend % 100);
        BigDecimal subtractionDays = BigDecimal.valueOf(subtraction % 100);
        BigDecimal fractionalPart = (minuendDays.subtract(subtractionDays)).divide(
                BigDecimal.valueOf(31), 2, BigDecimal.ROUND_HALF_UP);
        return integerPart.add(fractionalPart);
    }

    public static Long betweenMonthes(Integer before, Integer after) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        Calendar be = Calendar.getInstance();
        Calendar af = Calendar.getInstance();

        try {
            be.setTime(simpleDateFormat.parse(String.valueOf(before)));
            af.setTime(simpleDateFormat.parse(String.valueOf(after)));
        } catch (ParseException var9) {
            var9.printStackTrace();
        }

        long inOneYear = (long)(af.get(2) - be.get(2));
        long outOneYear = (long)((af.get(1) - be.get(1)) * 12);
        return Math.abs(inOneYear + outOneYear);
    }

    public static Boolean smaller(Integer val1, Integer val2) {
        Integer defaultValue = 0;
        Integer tmpVal1 = nvl(val1, defaultValue);
        Integer tmpVal2 = nvl(val2, defaultValue);
        return (tmpVal1.compareTo(tmpVal2) < 0);
    }

    public static <T> T nvl(T srcValue, T replaceValue) {
        return isEmpty(srcValue) ? replaceValue : srcValue;
    }

    public static boolean isEmpty(Object obj) {
        if (obj == null) {
            return true;
        } else if (obj instanceof CharSequence) {
            return ((CharSequence)obj).length() == 0;
        } else if (obj.getClass().isArray()) {
            return Array.getLength(obj) == 0;
        } else if (obj instanceof Collection) {
            return ((Collection)obj).isEmpty();
        } else {
            return obj instanceof Map ? ((Map)obj).isEmpty() : false;
        }
    }

}
